home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / gblanker36_src.lha / GSource.lha / GSource / MUI / PrefInterp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-03  |  16.0 KB  |  540 lines

  1. #include <exec/memory.h>
  2. #include <libraries/mui.h>
  3.  
  4. #include <proto/exec.h>
  5. #include <proto/dos.h>
  6. #include <proto/intuition.h>
  7. #include <proto/muimaster.h>
  8. #include <proto/asl.h>
  9.  
  10. #include "/Libraries/Garshnelib/Garshnelib_protos.h"
  11. #include "/Libraries/Garshnelib/Garshnelib_pragmas.h"
  12.  
  13. #include <clib/alib_protos.h>
  14.  
  15. #include <string.h>
  16.  
  17. #include "PrefInterp_rev.h"
  18. #include "PrefInterp.h"
  19. #include "/protos/parse.h"
  20. #include "/defs.h"
  21.  
  22. #define MAKE_ID(a,b,c,d)\
  23. ((ULONG)(a)<<24|(ULONG)(b)<<16|(ULONG)(c)<<8|(ULONG)(d))
  24. #define SIG_REPLY ( 1L << ReplyPort->mp_SigBit )
  25.  
  26. struct IntuitionBase *IntuitionBase;
  27. struct Library *MUIMasterBase, *GarshnelibBase;
  28. Object *ModuleApp, *ModuleWnd, **Objects;
  29. struct MsgPort *ReplyPort = 0L;
  30. ULONG ModuleSigs = 0L;
  31. LONG NumGadgets;
  32. VOID *Memory = 0L;
  33. PrefObject *Prefs;
  34.  
  35. STRPTR LToStr( LONG Value )
  36. {
  37.     static BYTE Buffer[16];
  38.     STRPTR Ptr = &Buffer[15];
  39.  
  40.     if( !Value )
  41.         return "0";
  42.  
  43.     *Ptr = '\0';
  44.  
  45.     while( Value )
  46.     {
  47.         *(--Ptr) = ( Value % 10 ) + '0';
  48.         Value /= 10;
  49.     }
  50.  
  51.     return Ptr;
  52. }
  53.  
  54. STRPTR Ununderscore( STRPTR String )
  55. {
  56.     static BYTE Buffer[128];
  57.     LONG i = 0;
  58.  
  59.     while( *String )
  60.     {
  61.         if( *String != '_' )
  62.             Buffer[i++] = *String;
  63.         String++;
  64.     }
  65.     Buffer[i] = '\0';
  66.  
  67.     return Buffer;
  68. }
  69.  
  70. VOID FontRequest( struct Window *Wnd, struct TextAttr *Attr )
  71. {
  72.     struct Library *AslBase = OpenLibrary( "asl.library", 37L );
  73.     struct FontRequester *fReq;
  74.  
  75.     if( !AslBase )
  76.         return;
  77.     
  78.     fReq = MUI_AllocAslRequestTags( ASL_FontRequest,
  79.                                ASL_FontName, Attr->ta_Name,
  80.                                ASL_FontHeight, Attr->ta_YSize,
  81.                                ASL_MaxHeight, 100, TAG_DONE );
  82.     if( fReq )
  83.     {
  84.         if( MUI_AslRequestTags( fReq,
  85.                            ASLFO_Window, Wnd,
  86.                            ASLFO_SleepWindow, TRUE,
  87.                            ASLFO_TitleText, ( LONG )"Please choose a font...",
  88.                            TAG_DONE ))
  89.         {
  90.             CopyMem( fReq->fo_Attr.ta_Name, Attr->ta_Name, 31 );
  91.             Attr->ta_YSize = fReq->fo_Attr.ta_YSize;
  92.         }
  93.         MUI_FreeAslRequest( fReq );
  94.     }
  95.     
  96.     CloseLibrary( AslBase );
  97. }   
  98.  
  99. VOID SendMessageToPort( LONG Type, STRPTR PortName )
  100. {
  101.     struct MsgPort *ForeignPort;
  102.     BlankMsg *ClientMsg;
  103.     
  104.     if( ForeignPort = FindPort( PortName ))
  105.     {
  106.         if( ClientMsg = AllocPooled( Memory, sizeof( BlankMsg )))
  107.         {
  108.             ClientMsg->bm_Mess.mn_ReplyPort = ReplyPort;
  109.             ClientMsg->bm_Mess.mn_Length = sizeof( BlankMsg );
  110.             ClientMsg->bm_Type = Type;
  111.             PutMsg( ForeignPort, ( struct Message * )ClientMsg );
  112.         }
  113.     }
  114. }
  115.  
  116. LONG main( LONG argc, STRPTR argv[] )
  117. {
  118.     Object *VertGroup, *SaveBtn, *TestBtn, *CancelBtn, *DisplayBtn, *CtrlGrp;
  119.     Object *NameInf, *SizeInf;
  120.     LONG i, j, LastNonGrp = 0, DispID = -1, Min, Max, *Types, rc, ID, Sigs;
  121.     BYTE DescripName[108], PrefsName[108], BogusBuf[128], ValidPrefs = FALSE;
  122.     STRPTR *Labels, *KeyStrs, AppName;
  123.     ULONG Args[] = { 0L, 0L }, GroupObjects = 0, *Tags, Exit = FALSE;
  124.     BPTR Descrip, Tmp;
  125.     Object **Indics;
  126.  
  127.     if( argc != 2 )
  128.         return RETURN_WARN;
  129.     
  130.     if( FindPort( "GarshnePrefs" ))
  131.         return RETURN_WARN;
  132.  
  133.     IntuitionBase = ( struct IntuitionBase * )
  134.         OpenLibrary( "intuition.library", 37 );
  135.     MUIMasterBase = OpenLibrary( MUIMASTER_NAME, MUIMASTER_VMIN );
  136.     GarshnelibBase = OpenLibrary( "Garshnelib.library", 37 );
  137.  
  138.     if( !IntuitionBase || !MUIMasterBase || !GarshnelibBase )
  139.         goto JAIL;
  140.     
  141.     if(!( Memory = CreatePool( MEMF_CLEAR, 1024, 512 )))
  142.         goto JAIL;
  143.  
  144.     if(!( ReplyPort = CreatePort( "GarshnePrefs", 0L )))
  145.         goto JAIL;
  146.  
  147.     strcpy( DescripName, argv[1] );
  148.     strcat( DescripName, ".ifc" );
  149.  
  150.     strcpy( PrefsName, argv[1] );
  151.     strcat( PrefsName, ".prefs" );
  152.     
  153.     if(!( Descrip = Open( DescripName, MODE_OLDFILE )))
  154.         goto JAIL;
  155.  
  156.     NumGadgets = ScanDigit( Descrip );
  157.  
  158.     Objects = AllocPooled( Memory, sizeof( Object * ) * ( NumGadgets + 1 ));
  159.     Indics = AllocPooled( Memory, sizeof( Object * ) * ( NumGadgets + 1 ));
  160.     Prefs = AllocPooled( Memory, sizeof( PrefObject ) * ( NumGadgets + 1 ));
  161.     Types = AllocPooled( Memory, sizeof( LONG ) * ( NumGadgets + 1 ));
  162.     KeyStrs = AllocPooled( Memory, sizeof( STRPTR ) * ( NumGadgets + 1 ));
  163.     
  164.     if( !Objects || !Indics || !Prefs || !Types || !KeyStrs )
  165.         goto PREJAIL;
  166.  
  167.     AppName = FilePart( argv[1] );
  168.  
  169.     if( Tmp = Open( PrefsName, MODE_OLDFILE ))
  170.     {
  171.         Read( Tmp, Prefs, sizeof( LONG )); /* Ignore entry count */
  172.         if( Read( Tmp, Prefs, sizeof( PrefObject ) * NumGadgets ) ==
  173.            sizeof( PrefObject ) * NumGadgets )
  174.             ValidPrefs = TRUE;
  175.         Close( Tmp );
  176.     }
  177.  
  178.     for( i = 0; i < NumGadgets; i++ )
  179.     {
  180.         STRPTR LabelStr;
  181.         
  182.         switch( Prefs[i].po_Type = Types[i] = ScanType( Descrip ))
  183.         {
  184.         case GAD_CYCLE:
  185.             LabelStr = Ununderscore( ScanToken( Descrip ));
  186.             KeyStrs[i] = ScanToken( Descrip );
  187.             Labels = ScanTokenArray( Descrip );
  188.             if( ValidPrefs )
  189.                 ScanDigit( Descrip );
  190.             else
  191.                 Prefs[i].po_Active = ScanDigit( Descrip );
  192.             Indics[i] = KeyLabel( LabelStr, *KeyStrs[i] );
  193.             Objects[i] = CycleObject,
  194.                 MUIA_Cycle_Active, Prefs[i].po_Active,
  195.                 MUIA_Cycle_Entries, Labels,
  196.                 MUIA_ControlChar, *KeyStrs[i],
  197.             End;
  198.             break;
  199.         case GAD_SLIDER:
  200.             LabelStr = Ununderscore( ScanToken( Descrip ));
  201.             KeyStrs[i] = ScanToken( Descrip );
  202.             Min = ScanDigit( Descrip );
  203.             Max = ScanDigit( Descrip );
  204.             if( ValidPrefs )
  205.                 ScanDigit( Descrip );
  206.             else
  207.                 Prefs[i].po_Level = ScanDigit( Descrip );
  208.             Indics[i] = KeyLabel( LabelStr, *KeyStrs[i] );
  209.             Objects[i] = KeySlider( Min, Max, Prefs[i].po_Level, *KeyStrs[i] );
  210.             break;
  211.         case GAD_FONT:
  212.             if( ValidPrefs )
  213.             {
  214.                 ScanToken( Descrip );
  215.                 ScanDigit( Descrip );
  216.             }
  217.             else
  218.             {
  219.                 strcpy( Prefs[i].po_Name, ScanToken( Descrip ));
  220.                 Prefs[i].po_Attr.ta_YSize = ScanDigit( Descrip );
  221.             }
  222.             Prefs[i].po_Attr.ta_Name = Prefs[i].po_Name;
  223.             Objects[i] = KeyButton( "Font", 'f' );
  224.             Objects[i] = TextObject,
  225.                 ButtonFrame,
  226.                 MUIA_Text_Contents, "Font",
  227.                 MUIA_Text_PreParse, "\33c",
  228.                 MUIA_Text_HiChar, 'f',
  229.                 MUIA_ControlChar, 'f',
  230.                 MUIA_InputMode, MUIV_InputMode_RelVerify,
  231.                 MUIA_Background, MUII_ButtonBack,
  232.                 MUIA_HorizWeight, 20,
  233.             End;
  234.             Args[0] = Prefs[i].po_Attr.ta_YSize,
  235.             Indics[i] = HGroup,
  236.                 Child, NameInf = TextObject,
  237.                     ReadListFrame,
  238.                     MUIA_Text_Contents, Prefs[i].po_Name,
  239.                     MUIA_HorizWeight, 70,
  240.                     MUIA_Background, MUII_BACKGROUND,
  241.                 End,
  242.                 Child, SizeInf = TextObject,
  243.                     ReadListFrame,
  244.                     MUIA_Text_Contents, LToStr( Prefs[i].po_Attr.ta_YSize ),
  245.                     MUIA_HorizWeight, 10,
  246.                     MUIA_Background, MUII_BACKGROUND,
  247.                 End,
  248.             End;
  249.             break;
  250.         case GAD_STRING:
  251.             LabelStr = Ununderscore( ScanToken( Descrip ));
  252.             KeyStrs[i] = ScanToken( Descrip );
  253.             if( ValidPrefs )
  254.                 FGets( Descrip, BogusBuf, 128 );
  255.             else
  256.             {
  257.                 FGets( Descrip, Prefs[i].po_Value, 128 );
  258.                 Prefs[i].po_Value[strlen( Prefs[i].po_Value )-1] = '\0';
  259.             }
  260.             Indics[i] = KeyLabel( LabelStr, *KeyStrs[i] );
  261.             Objects[i] = KeyString( Prefs[i].po_Value, 127, *KeyStrs[i] );
  262.             break;
  263.         case GAD_DISPLAY:
  264.             DispID = i + 10;
  265.             if( ValidPrefs )
  266.                 ScanDigit( Descrip );
  267.             else
  268.             {
  269.                 Prefs[i].po_ModeID = getTopScreenMode();
  270.                 Prefs[i].po_Depth = ScanDigit( Descrip );
  271.             }
  272.             break;
  273.         case GAD_DELIM:
  274.             Tags = AllocPooled( Memory,
  275.                                4 * sizeof( LONG * ) * ( i - LastNonGrp ) + 3 );
  276.             if( Tags )
  277.             {
  278.                 LONG n = 0;
  279.  
  280.                 Tags[n++] = MUIA_Group_Horiz;
  281.                 Tags[n++] = TRUE;
  282.                 for( j = LastNonGrp; j < i; j++ )
  283.                 {
  284.                     Tags[n++] = MUIA_Group_Child;
  285.                     Tags[n++] = ( Types[j] == GAD_FONT ) ?
  286.                         ( ULONG )Objects[j] : ( ULONG )Indics[j];
  287.                     Tags[n++] = MUIA_Group_Child;
  288.                     Tags[n++] = ( Types[j] == GAD_FONT ) ?
  289.                         ( ULONG )Indics[j] : ( ULONG )Objects[j];
  290.                 }
  291.                 Tags[n] = TAG_END;
  292.                 Objects[i] = MUI_NewObjectA( MUIC_Group,
  293.                                            ( struct TagItem * )Tags );
  294.                 FreePooled( Memory, Tags,
  295.                            4 * sizeof( LONG * ) * ( i - LastNonGrp ) + 3 );
  296.                 LastNonGrp = i+1;
  297.                 GroupObjects += 1;
  298.             }
  299.             break;
  300.         default:
  301.             break;
  302.         }
  303.     }
  304.     
  305.     if( Descrip )
  306.     {
  307.         Close( Descrip );
  308.         Descrip = 0L;
  309.     }
  310.  
  311.     if( DispID != -1 )
  312.     {
  313.         CtrlGrp = HGroup,
  314.             Child, SaveBtn = KeyButton( "Save", 's' ),
  315.             Child, TestBtn = KeyButton( "Test", 't' ),
  316.             Child, DisplayBtn = KeyButton( "Display", 'd' ),
  317.             Child, CancelBtn = KeyButton( "Cancel", 'c' ),
  318.         End;
  319.     }
  320.     else
  321.     {
  322.         CtrlGrp = HGroup,
  323.             Child, SaveBtn = KeyButton( "Save", 's' ),
  324.             Child, TestBtn = KeyButton( "Test", 't' ),
  325.             Child, CancelBtn = KeyButton( "Cancel", 'c' ),
  326.         End;
  327.     }
  328.  
  329.     if( Tags = AllocPooled( Memory, sizeof( LONG * ) * GroupObjects * 2 + 3 ))
  330.     {
  331.         LONG n = 0;
  332.  
  333.         for( i = 0; i < NumGadgets; i++ )
  334.         {
  335.             if( Types[i] == GAD_DELIM )
  336.             {
  337.                 Tags[n++] = MUIA_Group_Child;
  338.                 Tags[n++] = ( ULONG )Objects[i];
  339.             }
  340.         }
  341.         Tags[n++] = MUIA_Group_Child;
  342.         Tags[n++] = ( ULONG )CtrlGrp;
  343.         Tags[n] = TAG_END;
  344.         VertGroup = MUI_NewObjectA( MUIC_Group, ( struct TagItem * )Tags );
  345.         FreePooled( Memory, Tags, sizeof( LONG * ) * GroupObjects * 2 + 3 );
  346.     }
  347.  
  348.     ModuleApp = ApplicationObject,
  349.         MUIA_Application_Title, "Garshneprefs",
  350.         MUIA_Application_Version, VERS,
  351.         MUIA_Application_Copyright, "Free Software",
  352.         MUIA_Application_Author, "Michael D. Bayne",
  353.         MUIA_Application_Description, "Module preferences interpreter",
  354.         MUIA_Application_Base, "GPREFS",
  355.         MUIA_Application_Window, ModuleWnd = WindowObject,
  356.             MUIA_Window_ID, MAKE_ID( AppName[0], AppName[1], AppName[2],
  357.                 AppName[3]  ),
  358.             MUIA_Window_ScreenTitle, AppName,
  359.             MUIA_Window_Title, AppName,
  360.             MUIA_Window_RootObject, VertGroup,
  361.         End,
  362.     End;
  363.     
  364.     if( !ModuleApp )
  365.         goto PREJAIL;
  366.  
  367. #ifdef FUNKY_MUI
  368.     DoMethod( ModuleApp, MUIM_Application_Load,
  369.         MUIV_Application_Load_ENVARC );
  370. #endif
  371.     DoMethod( ModuleWnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  372.         ModuleApp, 2, MUIM_Application_ReturnID,
  373.         MUIV_Application_ReturnID_Quit );
  374.     DoMethod( SaveBtn, MUIM_Notify, MUIA_Pressed, FALSE, ModuleApp, 2,
  375.         MUIM_Application_ReturnID, ID_SAVE );
  376.     DoMethod( TestBtn, MUIM_Notify, MUIA_Pressed, FALSE, ModuleApp, 2,
  377.         MUIM_Application_ReturnID, ID_TEST );
  378.     if( DispID != -1 )
  379.         DoMethod( DisplayBtn, MUIM_Notify, MUIA_Pressed, FALSE, ModuleApp, 2,
  380.             MUIM_Application_ReturnID, DispID );
  381.     DoMethod( CancelBtn, MUIM_Notify, MUIA_Pressed, FALSE, ModuleApp, 2,
  382.         MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit );
  383.     
  384.     for( i = 0; i < NumGadgets; i++ )
  385.     {
  386.         switch( Types[i] )
  387.         {
  388.         case GAD_SLIDER:
  389.             DoMethod( Objects[i], MUIM_Notify, MUIA_Slider_Level,
  390.                      MUIV_EveryTime, ModuleApp, 2, MUIM_Application_ReturnID,
  391.                      i + 10 );
  392.             break;
  393.         case GAD_CYCLE:
  394.             DoMethod( Objects[i], MUIM_Notify, MUIA_Cycle_Active,
  395.                      MUIV_EveryTime, ModuleApp, 2, MUIM_Application_ReturnID,
  396.                      i + 10 );
  397.             break;
  398.         case GAD_STRING:
  399.             DoMethod( Objects[i], MUIM_Notify, MUIA_String_Contents,
  400.                      MUIV_EveryTime, ModuleApp, 2, MUIM_Application_ReturnID,
  401.                      i + 10 );
  402.             break;
  403.         case GAD_FONT:
  404.             DoMethod( Objects[i], MUIM_Notify, MUIA_Pressed, FALSE, ModuleApp,
  405.                 2, MUIM_Application_ReturnID, i + 10 );
  406.             break;
  407.         default:
  408.             break;
  409.         }
  410.     }
  411.     SetAttrs( ModuleWnd, MUIA_Window_Open, TRUE, TAG_DONE );
  412.     while( ModuleApp && !Exit )
  413.     {
  414.         rc = DoMethod( ModuleApp, MUIM_Application_Input, &ModuleSigs );
  415.         switch( rc )
  416.         {
  417.         case ID_SAVE:
  418.             if( Tmp = Open( PrefsName, MODE_NEWFILE ))
  419.             {
  420.                 Write( Tmp, &NumGadgets, sizeof( LONG ));
  421.                 Write( Tmp, Prefs, sizeof( PrefObject ) * NumGadgets );
  422.                 Close( Tmp );
  423.             }
  424.             SendMessageToPort( BM_RELOADPREFS, "GarshneClient" );
  425.         case MUIV_Application_ReturnID_Quit:
  426.             Exit = TRUE;
  427.             break;
  428.         case ID_TEST:
  429.             if( Tmp = Open( "T:GBlankerTmpPrefs", MODE_NEWFILE ))
  430.             {
  431.                 Write( Tmp, &NumGadgets, sizeof( LONG ));
  432.                 Write( Tmp, Prefs, sizeof( PrefObject ) * NumGadgets );
  433.                 Close( Tmp );
  434.                 SendMessageToPort( BM_SENDTEST, "GarshneServer" );
  435.             }
  436.             break;
  437.         default:
  438.             ID = rc - 10;
  439.             if( rc > 9 && ID < NumGadgets )
  440.             {
  441.                 switch( Types[ID] )
  442.                 {
  443.                 case GAD_CYCLE:
  444.                     GetAttr( MUIA_Cycle_Active, Objects[ID],
  445.                         ( ULONG * )&Prefs[ID].po_Active );
  446.                     break;
  447.                 case GAD_SLIDER:
  448.                     GetAttr( MUIA_Slider_Level, Objects[ID],
  449.                         ( ULONG * )&Prefs[ID].po_Level );
  450.                     break;
  451.                 case GAD_STRING:
  452.                 {
  453.                     STRPTR TmpPtr;
  454.  
  455.                     GetAttr( MUIA_String_Contents, Objects[ID],
  456.                             ( ULONG * )&TmpPtr );
  457.                     strcpy( Prefs[ID].po_Value, TmpPtr );
  458.                     break;
  459.                 }
  460.                 case GAD_FONT:
  461.                 {
  462.                     struct Window *Wnd;
  463.  
  464.                     GetAttr( MUIA_Window_Window, ModuleWnd, ( ULONG * )&Wnd );
  465.                     if( Wnd )
  466.                         FontRequest( Wnd, &Prefs[ID].po_Attr );
  467.                     SetAttrs( NameInf, MUIA_Text_Contents, Prefs[ID].po_Name,
  468.                              TAG_DONE );
  469.                     SetAttrs( SizeInf, MUIA_Text_Contents,
  470.                              LToStr( Prefs[ID].po_Attr.ta_YSize ), TAG_DONE );
  471.                     break;
  472.                 }
  473.                 case GAD_DISPLAY:
  474.                 {
  475.                     struct Window *Wnd;
  476.  
  477.                     GetAttr( MUIA_Window_Window, ModuleWnd, ( ULONG * )&Wnd );
  478.                     if( Wnd )
  479.                         ScreenModeRequest( Wnd, &Prefs[ID].po_ModeID,
  480.                             Prefs[ID].po_Depth ? &Prefs[ID].po_Depth : 0L );
  481.                     break;
  482.                 }
  483.                 default:
  484.                     break;
  485.                 }
  486.             }
  487.             break;
  488.         }
  489.  
  490.         if( ModuleSigs )
  491.             Sigs = Wait( ModuleSigs | SIG_REPLY | SIGBREAKF_CTRL_C );
  492.         else
  493.             continue;
  494.         
  495.         if( Sigs & SIG_REPLY )
  496.         {
  497.             BlankMsg *FreeMe;
  498.  
  499.             while( FreeMe = ( BlankMsg * )GetMsg( ReplyPort ))
  500.             {
  501.                 switch( FreeMe->bm_Type )
  502.                 {
  503.                 case BM_DOQUIT:
  504.                     FreeMe->bm_Flags |= BF_REPLY;
  505.                     ReplyMsg(( struct Message * )FreeMe );
  506.                     Exit = TRUE;
  507.                     break;
  508.                 case BM_RELOADPREFS:
  509.                 case BM_SENDTEST:
  510.                     FreePooled( Memory, FreeMe, sizeof( BlankMsg ));
  511.                     break;
  512.                 }
  513.             }
  514.         }
  515.  
  516.         if( Sigs & SIGBREAKF_CTRL_C )
  517.             Exit = TRUE;
  518.     }        
  519.  
  520.     DisposeObject( ModuleApp );
  521.     
  522.  PREJAIL:
  523.     if( Descrip )
  524.         Close( Descrip );
  525.  
  526.  JAIL:
  527.     if( ReplyPort )
  528.         DeletePort( ReplyPort );
  529.     if( Memory )
  530.         DeletePool( Memory );
  531.     if( GarshnelibBase )
  532.         CloseLibrary( GarshnelibBase );
  533.     if( MUIMasterBase )
  534.         CloseLibrary( MUIMasterBase );
  535.     if( IntuitionBase )
  536.         CloseLibrary(( struct Library * )IntuitionBase );
  537.  
  538.     return RETURN_OK;
  539. }
  540.